"use strict";

class NodeUtil {
    static getRelativePosition(node, targetNode) {
        var nodePos = (node.getParent() || node).convertToWorldSpaceAR(node.getPosition());
        return targetNode.convertToNodeSpaceAR(nodePos);
    }

    static isPosOnNodeRect(pos, targetNode) {
        return targetNode.getBoundingBoxToWorld().contains(pos);
    }

    static areNodesOverlap(node1, node2, ignoreRotation = false) {
        var rect1 = node1.getBoundingBoxToWorld();
        var rect2 = node2.getBoundingBoxToWorld();
        return ignoreRotation ? rect1.containsRect(rect2) : rect1.intersects(rect2);
    }

    static getNodeSelfBoundingBoxToWorld(node) {
        node.parent._updateWorldMatrix();
        var size = node.getContentSize();
        var width = size.width;
        var height = size.height;
        var anchor = node.getAnchorPoint();
        var boundingBox = cc.rect(-anchor.x * width, -anchor.y * height, width, height);
        node._calculWorldMatrix();
        boundingBox.transformMat4(boundingBox, node._worldMatrix);
        return boundingBox;
    }
}

export default NodeUtil;
